package net.tngou.tpage.action.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import org.apache.commons.lang3.math.NumberUtils;

import com.alibaba.fastjson.JSON;

import net.tngou.tpage.action.BaseAction;
import net.tngou.tpage.jdbc.OrderType;
import net.tngou.tpage.jdbc.PageUtil;
import net.tngou.tpage.pojo.Attribute;
import net.tngou.tpage.pojo.Element;
import net.tngou.tpage.pojo.Result;
import net.tngou.tpage.pojo.Tempdata;
import net.tngou.tpage.util.Pinyin4j;
import net.tngou.tpage.util.StringUtil;

/**
 * 元素
 * @author tngou@tngou.net
 *
 */
public class ElementApi extends BaseAction {
	/**
	 *公有元素
	 */
	@Override
	public void execute() throws ServletException, IOException {		
			if(user==null){
				run_nologin();return;
			}
			String keyword = ask.getKeyword();
			Map<String, Object> map = new HashMap<>();
			if(StringUtil.isNotEmpty(keyword))
			{
				map.put("id", keyword);
				map.put("name", keyword);
				map.put("pinyin", keyword+"%");
			}
			PageUtil page = service.getPage(ask.getPage(), ask.getRows(),"user=0",map, Element.class);
			printJson(toJsonPage(page));		
	}
	
	/**
	 * 私有元素
	 */
	public void my() {	
		if(user==null){
			run_nologin();return;
		}
		String keyword = ask.getKeyword();
		Map<String, Object> map = new HashMap<>();
		if(StringUtil.isNotEmpty(keyword))
		{
			map.put("id", keyword);
			map.put("name", keyword);
			map.put("pinyin", keyword+"%");
		}
		PageUtil page = service.getPage(ask.getPage(), ask.getRows(),"user="+user.getId(),map, Element.class);
		printJson(toJsonPage(page));		
	}
	
	/**
	 * 列表 更新类型 取得元素
	 */
	public void list() {
		if(user==null){
			run_nologin();return;
		}
		String attribute = request.getParameter("attribute");
		if(isEmpty(attribute)||!NumberUtils.isNumber(attribute))
		{
			run_false("请求的参数不正确！");return;	
		}
		List<?> list = service.getList("attribute="+attribute+ " AND user IN(0,"+user.getId()+")", Element.class);
		printJson(toJsonP(list));
		
	}
	/**
	 * 属性列表
	 */
	public void attribute() {
		if(user==null){
			run_nologin();return;
		}
		List<?> list = service.getList("seq",OrderType.DESC,Attribute.class);		
		printJson(toJsonP(ask.getCallback(), JSON.toJSONString(list)));
	}
	
	/**
	 * 添加元素
	 */
	public void add() {
		if(user==null){
			run_nologin();return;
		}
		Element bean = getAsk(Element.class);
		if(isEmpty(bean.getName()))
		{
			run_false("请求的参数不正确！");return;	
		}
		if(user.getRole()==0)
		{
			bean.setUser(user.getId());
		}else
		{
			bean.setUser(0);
		}
		bean.setPinyin(Pinyin4j.ToHanyuPinyinHead(bean.getName(), true));//设置拼音	
		long b = bean.save();
		if(b>0){	
			run_success("添加成功！",b);
		}else {run_false("添加失败！");return;}
	}
	
	/**
	 * 更新
	 */
	public void update() {
		if(user==null){
			run_nologin();return;
		}
		Element bean = getAsk(Element.class);
		if(ask.getId()==0)
		{
			run_false("请求的参数不正确！");return;	
		}
		Element element = bean.get(ask.getId());
		if(element==null)
		{
			run_false("请求的数据不正确！");return;	
		}
		if(!((element.getUser()==0&&user.getRole()==0)||element.getUser()==user.getId())){
			run_false("没有修改权限！");return;	
		}
		if(StringUtil.isNotEmpty(bean.getName()))element.setName(bean.getName());
		if(StringUtil.isNotEmpty(bean.getPinyin()))element.setPinyin(bean.getPinyin().toLowerCase());
		if(StringUtil.isNotEmpty(bean.getDescription()))element.setDescription(bean.getDescription());
		if(StringUtil.isNotEmpty(request.getParameter("attribute ")))element.setAttribute (bean.getAttribute ());
		if(StringUtil.isNotEmpty(request.getParameter("beasedata")))element.setBeasedata(bean.getBeasedata());
		long b = element.update();
		if(b>0){	
			run_success("修改成功！");
		}else {run_false("修改失败！");return;}
	}
	
	
	/**
	 * 删除
	 */
	public void delete() {
		if(user==null){
			run_nologin();return;
		}
		if(ask.getId()==0)
		{
			run_false("请求的参数不正确！");return;	
		}
		Element element = new Element().get(ask.getId());
		if(element==null)
		{
			run_false("请求的数据不正确！");return;	
		}
		if(!((element.getUser()==0&&user.getRole()==0)||element.getUser()==user.getId())){
			run_false("没有删除权限！");return;	
		}		
		int total=new Tempdata().totalCount("element="+element.getId())+new Result().totalCount("element"+element.getId());
		if(total>0)
	    {
	    	run_false("当前已有绑定数据,不能进行删除！");return;	
	    }
		boolean b = element.delete();
		if(b){	
			run_success("删除成功！");
		}else {run_false("删除失败！");return;}
	}
	
	/**
	 * 
	 */
	public void copy() {
		if(user==null){
			run_nologin();return;
		}
		if(ask.getId()==0)
		{
			run_false("请求的参数不正确！");return;	
		}
		Element element = new Element().get(ask.getId());
		if(element==null)
		{
			run_false("请求的数据不正确！");return;	
		}
		long userid=0;
		if(user.getRole()!=0)userid=user.getId();
		element.setId(0);
		element.setUser(userid);
		long b = element.save();
		if(b>0){	
			run_success("复制成功！");
		}else {run_false("复制失败！");return;}
	}
	
	
}
